package com.nickstumpos.android.GmailWidgetPlus.Configure.OptionsList.options;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ListView;

import com.nickstumpos.android.GmailWidgetPlus.Configure.OptionsList.OptionsList;
import com.nickstumpos.android.GmailWidgetPlus.Constants.GMWP_Constants;
import com.nickstumpos.android.GmailWidgetPlus.widget.sizes.WidgetSize;
import com.nickstumpos.android.GmailWidgetPlus.widget.skins.WidgetSkin;
import com.nickstumpos.android.GmailWidgetPlus.widget.skins.WidgetSkins;

public class SkinsOptionList extends OptionsList {

	private List<WidgetSkin>	choicelist;

	public SkinsOptionList(final Context context,
			final int widgetId,
			final String title,
			final WidgetSize size) {

		super(context, widgetId, title);
		this.setupDialog(size);
	}

	@Override
	protected void save() {

		WidgetSkin theme = WidgetSkins.TWO_BY_TWO_SIMPLE;
		theme = this.choicelist.get(this.getCheckedItemPosition() - 1);
		final SharedPreferences.Editor prefs = this.getPrefs().edit();
		prefs.putString(GMWP_Constants.WIDGET_SKIN + this.getWidgetId(),
				theme.toString());
		prefs.commit();
		this.setVisibility(View.GONE);
	}

	@Override
	protected void setupDialog() {

	}

	public void setupDialog(final WidgetSize size) {

		this.choicelist = new ArrayList<WidgetSkin>();
		int defaultSkin = 1;
		int position = 1;
		for (final WidgetSkin widget : WidgetSkins.values()) {
			if (widget.getSupportedSizes().indexOf(size) > -1) {
				this.getAa().add(this.getContext()
						.getString(widget.getNameId()));
				this.choicelist.add(widget);
				if (widget.equals(WidgetSkins.TWO_BY_TWO_SIMPLE)) {
					defaultSkin = position;
				}
				position++;
			}
		}
		this.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		final String widget = this.getPrefs()
				.getString(GMWP_Constants.WIDGET_SKIN + this.getWidgetId(),
						null);
		if (widget == null) {
			this.setItemChecked(defaultSkin, true);
		} else {
			final int pos = this.choicelist.indexOf(WidgetSkins.valueOf(widget)) + 1;
			this.setItemChecked(pos, true);
		}
	}
}
